######### Notes ###########

# Files: Death Penalty - Corporal Punishment - Punitiveness,
#        Fear of Crime - Security Behaviour - Satisfaction
# Columns: Varname - Date - Index - Demographic - Poll - Question

######### Gallup ###########

library(tidyverse)


read_csv("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Survey Data/Finished/Gallup_allrepeatedcrimequestions.csv") %>%
  mutate(GP_CrimeNat = ifelse(GP_CrimeNat %in% c('increasing'),1,
                              ifelse(is.na(GP_CrimeNat), NA_integer_, 0)),
         GP_GovLongerSentences = ifelse(GP_GovLongerSentences %in% c('fairly important that it should be done',
                                                                     'very important that it should be done',
                                                                     'yes'),1,
                                        ifelse(is.na(GP_GovLongerSentences), NA_integer_, 0)),
         GP_DeathPen78 = ifelse(GP_DeathPen78 %in% c('should not be re-introduced at all'),0,
                                        ifelse(is.na(GP_DeathPen78), NA_integer_, 1)),
         GP_CrimeLocal = ifelse(GP_CrimeLocal %in% c('increased'),1,
                              ifelse(is.na(GP_CrimeLocal), NA_integer_, 0)),
         GP_SentTooShort = ifelse(GP_SentTooShort %in% c('too short'),1,
                                ifelse(is.na(GP_SentTooShort), NA_integer_, 0))) -> readforanalysis

readforanalysis %>%
  group_by(Date) %>%
  summarise(across(where(is.numeric), mean, na.rm = TRUE)) %>%
  pivot_longer(-Date) %>%
  drop_na(value) %>%
  rename(Index = value, Varname = name) %>%
  mutate(Demographic = "All adults") %>%
  left_join(., readforanalysis %>%
              group_by(Date) %>%
              summarise(across(where(is.numeric), ~ n())) %>%
              pivot_longer(-Date) %>%
              drop_na(value) %>%
              rename(n = value, Varname = name)) %>%
  bind_rows(readforanalysis %>%
              drop_na(age) %>%
              group_by(age, Date) %>%
              summarise(across(where(is.numeric), mean, na.rm = TRUE)) %>%
              pivot_longer(-c(Date, age)) %>%
              drop_na(value) %>%
              rename(Index = value, Varname = name, Demographic = age) %>%
              left_join(., readforanalysis %>%
                          drop_na(age) %>%
                          group_by(age, Date) %>%
                          summarise(across(where(is.numeric), ~ n())) %>%
                          pivot_longer(-c(Date, age)) %>%
                          drop_na(value) %>%
                          rename(n = value, Varname = name, Demographic = age))) %>%
  bind_rows(readforanalysis %>%
              drop_na(gender) %>%
              group_by(gender, Date) %>%
              summarise(across(where(is.numeric), mean, na.rm = TRUE)) %>%
              pivot_longer(-c(Date, gender)) %>%
              drop_na(value) %>%
              rename(Index = value, Varname = name, Demographic = gender)%>%
              left_join(., readforanalysis %>%
                          drop_na(gender) %>%
                          group_by(gender, Date) %>%
                          summarise(across(where(is.numeric), ~ n())) %>%
                          pivot_longer(-c(Date, gender)) %>%
                          drop_na(value) %>%
                          rename(n = value, Varname = name, Demographic = gender))) %>%
  select(Varname, Date, Index, n, Demographic) %>%
  mutate(Poll = "Gallup") -> OutPut

OutPut <- bind_rows(OutPut, read_csv("/Users/matteo/Desktop/Prison Polling/Prison Data Internship/Gallup and YouGov/Gallup/OriginalGallupResults.csv"))

OutPut %>%
  filter(Varname %in% c('GP_GovLongerSentences','GP_CauseCrime','GP_SentTooShort',
                        'GP_SentencingPrinciple','GP_ConvictInnocent')) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/Gallup/Punitiveness.csv", row.names = FALSE)

OutPut %>%
  filter(Varname %in% c('GP_CrimeNat','GP_CrimeLocal','GP_WalkNight',
                        'GP_SocialProblemDrug',"GP_SocialProblemProstitution",
                        "GP_SocialProblemRape","GP_SocialProblemGang",
                        "GP_SocialProblemJuvenileDel","GP_SocialProblemViolence",
                        "GP_SocialProblemOrgCrime")) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/Gallup/FearCrime.csv", row.names = FALSE)

OutPut %>%
  filter(Varname %in% c("GP_DeathPenArmy","GP_DeathPenTerrorist",
                        "GP_DeathPenWoman","GP_DeathPenSurprise",
                        "GP_DeathPenDrunk","GP_DeathPenInsane",
                        "GP_DeathPenProvoke","GP_DeathPenFun",
                        "GP_DeathPenChildRape","GP_DeathPenRape",
                        "GP_Death_Abolish","GP_Death_Murder",
                        "GP_Death_Keep","GP_DeathPen78")) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/Gallup/DeathPenalty.csv", row.names = FALSE)

OutPut %>%
  filter(Varname %in% c("GP_BringBackCat","GP_BringBackBirching","GP_BringBackCaning",
                        "GP_DontAbolishCorpPunish","GP_BringBackCorpPun",
                        "GP_TeachersCane","GP_ParentSlap")) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/Gallup/CorporalPunishment.csv", row.names = FALSE)

OutPut %>%
  filter(Varname %in% c("GP_LawNotEfficient","GP_LawNotFair","GP_ConvictInnocent",
                        "GP_FavourRich","GP_PoliceIneff","GP_PoliceCorrupt",
                        "GP_PoliceUneasy")) %>%
  write.csv(., "/Users/matteo/Desktop/Prison Polling/Dyad Ratio Ready/Gallup/Satisfaction.csv", row.names = FALSE)

